<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="勋章类型" prop="medalType">
        <el-select @change="handleQuery" clearable v-model="queryParams.medalType" placeholder="请选择">
          <el-option
            v-for="dict in sys_medal_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="勋章小图" prop="smallPicture">-->
<!--        <el-input-->
<!--          v-model="queryParams.smallPicture"-->
<!--          placeholder="请输入勋章小图"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="勋章大图" prop="largePicture">-->
<!--        <el-input-->
<!--          v-model="queryParams.largePicture"-->
<!--          placeholder="请输入勋章大图"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="点亮条件" prop="lightingCondition">-->
<!--        <el-input-->
<!--          v-model="queryParams.lightingCondition"-->
<!--          placeholder="请输入点亮条件"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="升级条件" prop="promotionCondition">-->
<!--        <el-input-->
<!--          v-model="queryParams.promotionCondition"-->
<!--          placeholder="请输入升级条件"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="点亮进度" prop="lightProgress">-->
<!--        <el-input-->
<!--          v-model="queryParams.lightProgress"-->
<!--          placeholder="请输入点亮进度"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="升级进度" prop="promotionProgress">-->
<!--        <el-input-->
<!--          v-model="queryParams.promotionProgress"-->
<!--          placeholder="请输入升级进度"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:medalConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:medalConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:medalConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:medalConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="medalConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="勋章类型" align="center" prop="medalType">
        <template #default="scope">
          <dict-tag :options="sys_medal_type" :value="scope.row.medalType" />
        </template>
      </el-table-column>
      <el-table-column label="勋章名称" align="center" prop="medalKey">
        <template #default="scope">
          <dict-tag :options="sys_medal_identity" :value="scope.row.medalKey" />
        </template>
      </el-table-column>
      <el-table-column label="勋章小图" align="center" prop="smallPicture">
        <template #default="scope">
          <ImagePreview
            :width="100" :height="100"
            :src="scope.row.smallPicture"
            :preview-src-list="[scope.row.smallPicture]"/>
        </template>
      </el-table-column>
      <el-table-column label="勋章大图" align="center" prop="largePicture">
        <template #default="scope">
          <ImagePreview
            :width="100" :height="100"
            :src="scope.row.largePicture"
            :preview-src-list="[scope.row.largePicture]"/>
        </template>
      </el-table-column>
      <el-table-column label="未点亮图片" align="center" prop="unlitPicture">
        <template #default="scope">
          <ImagePreview
            :width="100" :height="100"
            :src="scope.row.unlitPicture"
            :preview-src-list="[scope.row.unlitPicture]"/>
        </template>
      </el-table-column>
      <el-table-column label="点亮条件" align="center" prop="lightingCondition" />
      <el-table-column label="升级条件" align="center" prop="promotionCondition" />
      <el-table-column label="点亮进度" align="center" prop="lightProgress" />
      <el-table-column label="升级进度" align="center" prop="promotionProgress" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:medalConfig:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:medalConfig:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改勋章配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="medalConfigRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="勋章类型" prop="medalType">
          <el-select clearable v-model="form.medalType" placeholder="请选择">
            <el-option
              v-for="dict in sys_medal_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择勋章" prop="medalKey">
          <el-select clearable v-model="form.medalKey" placeholder="请选择">
            <el-option
              v-for="dict in sys_medal_identity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.medalType == '1'" label="勋章小图" prop="smallPicture">
          <image-uploads v-model="form.smallPicture" :limit="1"></image-uploads>
        </el-form-item>
        <el-form-item label="勋章大图" prop="largePicture">
          <image-uploads v-model="form.largePicture" :limit="1"></image-uploads>
        </el-form-item>
        <el-form-item label="未点亮图" prop="unlitPicture">
          <image-uploads v-model="form.unlitPicture" :limit="1"></image-uploads>
        </el-form-item>
        <el-form-item label="点亮条件" prop="lightingCondition">
          <el-input v-model="form.lightingCondition" placeholder="请输入点亮条件" />
        </el-form-item>
        <div v-if="form.medalType == '1'">
          <el-form-item label="升级条件" prop="promotionCondition">
            <el-input v-model="form.promotionCondition" placeholder="请输入升级条件" />
          </el-form-item>
          <el-form-item label="点亮进度" prop="lightProgress">
            <el-input-number v-model="form.lightProgress" controls-position="right" :min="0" />
          </el-form-item>
          <el-form-item label="升级进度" prop="promotionProgress">
            <el-input-number v-model="form.promotionProgress" controls-position="right" :min="0" />
          </el-form-item>
        </div>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MedalConfig">
import { listMedalConfig, getMedalConfig, delMedalConfig, addMedalConfig, updateMedalConfig } from "@/api/business/medalConfig";
import ImageUploads from "@/components/ImageUploads/index.vue";

const { proxy } = getCurrentInstance();
const {sys_medal_type,sys_medal_identity } = proxy.useDict("sys_medal_type","sys_medal_identity");
const medalConfigList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    medalType: '1',
    medalKey: undefined,
    smallPicture: undefined,
    largePicture: undefined,
    lightingCondition: undefined,
    promotionCondition: undefined,
    lightProgress: undefined,
    promotionProgress: undefined,
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    medalType: [
      { required: true, message: "勋章类型不能为空", trigger: "change" }
    ],
    medalKey: [
      { required: true, message: "勋章键值不能为空", trigger: "blur" }
    ],
    smallPicture: [
      { required: true, message: "勋章小图不能为空", trigger: "blur" }
    ],
    largePicture: [
      { required: true, message: "勋章大图不能为空", trigger: "blur" }
    ],
    lightingCondition: [
      { required: true, message: "点亮条件不能为空", trigger: "blur" }
    ],
    promotionCondition: [
      { required: true, message: "升级条件不能为空", trigger: "blur" }
    ],
    lightProgress: [
      { required: true, message: "点亮进度不能为空", trigger: "blur" }
    ],
    promotionProgress: [
      { required: true, message: "升级进度不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询勋章配置列表 */
function getList() {
  loading.value = true;
  listMedalConfig(queryParams.value).then(response => {
    medalConfigList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    medalType: '1',
    medalKey: null,
    smallPicture: null,
    largePicture: null,
    lightingCondition: null,
    promotionCondition: null,
    lightProgress: null,
    promotionProgress: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null
  };
  proxy.resetForm("medalConfigRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加勋章配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getMedalConfig(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改勋章配置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["medalConfigRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateMedalConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addMedalConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除勋章配置编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delMedalConfig(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/medalConfig/export', {
    ...queryParams.value
  }, `medalConfig_${new Date().getTime()}.xlsx`)
}

getList();
</script>
